package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.clinic.ClinicEventInfo;
import com.yanqu.road.entity.config.fishpond.FishPondEventInfo;
import com.yanqu.road.entity.config.npc.ManorNpcTalkInfo;
import com.yanqu.road.entity.config.npc.NpcTalkInfo;
import com.yanqu.road.entity.config.npc.NpcTradeInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NpcConfigDaoImpl extends TempDao {

    public Map<Integer, NpcTalkInfo> getNpcTalkInfoMap() {
        Map<Integer, NpcTalkInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_npc_event where `type` = 1 or `type` = 10";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                NpcTalkInfo data = new NpcTalkInfo();
                data.setEventId(rs.getInt("eventId"));
                data.setType(rs.getInt("type"));
                data.setWeight(rs.getInt("weight"));
                data.setReward(rs.getString("reward"));
                data.setExtendParam(rs.getString("extendParam"));


                dataMap.put(data.getEventId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, NpcTradeInfo> getNpcTradeInfoMap() {
        Map<Integer, NpcTradeInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_npc_event where `type` = 2";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);

            rs = call.executeQuery();
            while (rs.next()) {
                NpcTradeInfo data = new NpcTradeInfo();
                data.setEventId(rs.getInt("eventId"));
                data.setWeight(rs.getInt("weight"));
                data.setConsume(rs.getString("consume"));
                data.setReward(rs.getString("reward"));
                data.setTradeTime(rs.getInt("extendParam"));

                dataMap.put(data.getEventId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, NpcTradeInfo> getGanefenInfoMap() {
        Map<Integer, NpcTradeInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_npc_event where `type` = 13";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);

            rs = call.executeQuery();
            while (rs.next()) {
                NpcTradeInfo data = new NpcTradeInfo();
                data.setEventId(rs.getInt("eventId"));
                data.setWeight(rs.getInt("weight"));
                data.setConsume(rs.getString("consume"));
                data.setReward(rs.getString("reward"));
                data.setTradeTime(rs.getInt("extendParam"));

                dataMap.put(data.getEventId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, ClinicEventInfo> getClinicEventMap() {
        Map<Integer, ClinicEventInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_npc_event where `type` = 3";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);

            rs = call.executeQuery();
            while (rs.next()) {
                ClinicEventInfo data = new ClinicEventInfo();
                data.setEventId(rs.getInt("eventId"));
                data.setWeight(rs.getInt("weight"));
                data.setConsume(rs.getString("consume"));
                data.setReward(rs.getString("reward"));
                String extendParam = rs.getString("extendParam");
                if (!StringUtils.isNullOrEmpty(extendParam)) {
                    String[] split = extendParam.split(";");
                    data.setDiseaseId(Integer.parseInt(split[0]));
                    data.setNeedCureNum(Integer.parseInt(split[1]));
                    data.setNpcType(Integer.parseInt(split[2]));
                    data.setDiseaseWeight(Integer.parseInt(split[3]));
                }
                dataMap.put(data.getEventId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, ManorNpcTalkInfo> getManorNpcTalkInfo() {
        Map<Integer, ManorNpcTalkInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_npc_event where `type` = 4";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);

            rs = call.executeQuery();
            while (rs.next()) {
                ManorNpcTalkInfo data = new ManorNpcTalkInfo();
                data.setEventId(rs.getInt("eventId"));
                data.setWeight(rs.getInt("weight"));
                data.setReward(rs.getString("reward"));
                data.setGuestIdList(StringUtils.stringToIntegerList(rs.getString("extendParam"), ";"));
                dataMap.put(data.getEventId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public List<FishPondEventInfo> getFishPondEventInfoList() {
        List<FishPondEventInfo> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_npc_event where `type` >= 5 and `type` <= 7 ";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);

            rs = call.executeQuery();
            while (rs.next()) {
                FishPondEventInfo data = new FishPondEventInfo();
                data.setEventId(rs.getInt("eventId"));
                data.setType(rs.getInt("type"));
                data.setWeight(rs.getInt("weight"));
                data.setReward(rs.getString("reward"));
                data.setExtendParam((rs.getString("extendParam")));
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }
}
